/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package main;

import bean.RawMessage;
import bean.SMSCNodeConfiguration;
import java.io.FileInputStream;
import java.util.Properties;
import main.SMSCConnection.Status;

/**
 *
 * @author enemY
 */
public class Main {

    private static SMSCConnection conn;

    public static void main(String[] args) {
        SMSCNodeConfiguration cfg = loadConfiguration();

        conn = new SMSCConnection(cfg);
        conn.start();
        try {
            Thread.sleep(cfg.getReconnectInterval() * 1000 * 2);
        } catch (InterruptedException ex) {
            ex.printStackTrace();
        }
        while (true) {
            String[][] ss = functions.getResult("select * from smsc.schedule where last_status_id=0 limit 20");
            String sql = "";
            for (int i = 0; i < ss.length; i++) {
                Integer id = Integer.parseInt(ss[i][0]);
                sql += "update smsc.schedule set last_status_id=1 where id=" + id + ";\n";

            }
            System.out.println(sql);
            functions.execSql(sql);
            for (int i = 0; i < ss.length; i++) {
                Integer id = Integer.parseInt(ss[i][0]);
                SendSMS(ss[i][2], ss[i][3], id);
            }
            try {
                Thread.sleep(1000);
            } catch (Exception e) {
            }

        }
    }

    public static void updateStatus(int id, int status) {
        String sql = "update smsc.schedule set last_status_id=" + status + " where id=" + id;
        functions.execSql(sql);
    }

    public static void SendSMS(String Number, String Body, Integer id) {
        SMSCNodeConfiguration cfg = new SMSCNodeConfiguration();
        if (conn.getStatus() != Status.CONNECTED) {
            try {
                Thread.sleep(SMSCConnection.CONN_DEF_COMMS_TIMEOUT * 2);
            } catch (InterruptedException ex) {
            }
        }

        if (conn.getStatus() == Status.CONNECTED) {
            RawMessage msg = new RawMessage();
            msg.setRecipientMSISDN(Number);
            msg.setMessageType(RawMessage.MessageType.PLAIN);
            msg.setRawMessage(Body);
            conn.submitSM(msg, id, true);
        } else {
            updateStatus(id, -1);
        }
    }

    private static String getPropertie(Properties pr, String key, String defaultVal) {
        String result = pr.getProperty(key, defaultVal);
        result = result == null || result.trim().isEmpty() ? defaultVal : result;
        return result;

    }

    private static SMSCNodeConfiguration loadConfiguration() {
        Properties pr = new Properties();
        try {
            pr.load(new FileInputStream("configuration.properties"));
        } catch (Exception e) {
            System.out.println("Cannot load configuration, setting default values ");
//            pr.put("smpp.host", "91.151.128.64");
//            pr.put("smpp.port", "9022");
//            pr.put("db.string", "jdbc:postgresql://10.90.200.10:5432/gass");
//            pr.put("db.user", "gass");
//            pr.put("db.pass", "gilelumimani");
        }
        sets.db_string = getPropertie(pr, "db.string", "jdbc:postgresql://10.90.200.10:5432/gass");
        System.out.println(sets.db_string);
        sets.db_user = getPropertie(pr, "db.user", "gass");
        sets.db_pass = getPropertie(pr, "db.pass", "gilelumimani");
        SMSCNodeConfiguration cfg = new SMSCNodeConfiguration();
        cfg.setServerAddress(getPropertie(pr, "smpp.host", "91.151.128.64"));
        cfg.setServerPort(Integer.parseInt(getPropertie(pr, "smpp.port", "9022")));
        cfg.setSourceAddress("SOCAR");
        // user/pass 
        cfg.setSystemId("SocarSCR");
        cfg.setPassword("SC56r99");
        // timers
        cfg.setEnqLinkFreq(30);
        cfg.setReconnectInterval(5);
        // interface version 3.4
        cfg.setIfaceVersion(0x34);
        return cfg;
    }
}
